﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using ExtAspNet;
using MyDMS.DAL;
using ListItem = System.Web.UI.WebControls.ListItem;

namespace MyDMS_ExtJS.Admin
{
	public partial class FolderEdit : System.Web.UI.Page
    {
        private int _Folder_Code = 0;
        MyDMSEntity _myDmsEntity = new MyDMSEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null)
            {
                _Folder_Code = Convert.ToInt32(Request.QueryString["id"]);
            }
            if (!IsPostBack)
            {
                BindProject();
                var sf = _myDmsEntity.Folder.FirstOrDefault(s => s.Folder_Code == _Folder_Code);
                if (sf != null)
                {
                    var project = _myDmsEntity.Task.FirstOrDefault(p => p.Task_Code == sf.Task_Code);
                    BindTask(project.Project_Code);
                    ddlProject.SelectedValue = project.Project_Code.ToString();
                    tbName.Text = sf.Folder_Title;
                    tbDesc.Text = sf.Folder_Desc;
                    ddlTask.SelectedValue = sf.Task_Code.ToString();
                }
                else
                {
                    BindTask();
                }
            }
            if (_Folder_Code != 0)
            {
                ddlProject.Enabled = false;
            }
        }

        private void BindTask(int departmentCode = 0)
        {
            if (departmentCode != 0)
            {
                ddlTask.DataSource = _myDmsEntity.Task.Where(p => p.Project_Code == departmentCode).ToList();
            }
            else
            {
                ddlTask.DataSource = _myDmsEntity.Task.ToList();
            }
            ddlTask.DataValueField = "Task_Code";
            ddlTask.DataTextField = "Task_Title";
            ddlTask.DataBind();
            ddlTask.Items.Insert(0, new ExtAspNet.ListItem("选择小项", "-1"));
        }


        private void BindProject()
        {
            ddlProject.DataSource = _myDmsEntity.Project.ToList();
            ddlProject.DataTextField = "Project_Title";
            ddlProject.DataValueField = "Project_Code";
            ddlProject.DataBind();
            ddlProject.Items.Insert(0, new ExtAspNet.ListItem("选择中项", "-1"));
        }

        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            if (_Folder_Code != 0)
            {
                var sf = _myDmsEntity.Folder.FirstOrDefault(s => s.Folder_Code == _Folder_Code);
                if (sf != null)
                {
                    sf.Folder_Title = tbName.Text;
                    sf.Folder_Desc = tbDesc.Text;
                    sf.Updation_Date = DateTime.Now;
                    sf.Task_Code = Convert.ToInt32(ddlTask.SelectedValue);
                }
                _myDmsEntity.SaveChanges();
            }
            else
            {
                _myDmsEntity.Folder.AddObject(new MyDMS.DAL.Folder()
                {
                    Folder_Title = tbName.Text,
                    Folder_Desc = tbDesc.Text,
                    Updation_Date = DateTime.Now,
                    Creation_Date = DateTime.Now,
                    Creation_By = 0,
                    Task_Code = Convert.ToInt32(ddlTask.SelectedValue)
                });
                _myDmsEntity.SaveChanges();
            }

            // 2. 关闭本窗体，然后刷新父窗体
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }

        protected void ddlProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlTask.Items.Clear();
            BindTask(Convert.ToInt32(ddlProject.SelectedValue));
        }
    }
}